package com.bdqn.blog.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration //配置类
@EnableSwagger2// 开启Swagger2的自动配置
public class SwaggerConfig {

    //如果要设置多个分组 则设置多个docket
    @Bean
    public Docket docket1(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("group1");
    }
    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("group2");
    }
    @Bean
    public Docket docket3(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("group3");
    }

    @Bean //配置docket以配置Swagger具体参数
    public Docket docket(Environment environment) {

        // 设置要显示swagger的环境  判断当前是否处于该环境
        Profiles of = Profiles.of("dev", "test");
        // 通过 enable() 接收此参数判断是否要显示   如果是dev或者text环境则返回true
        boolean b = environment.acceptsProfiles(of);

        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                .groupName("阿桐幕") // 配置分组
                .enable(true) // 配置是否开启Swagger
                .select()  //通过select()方法，去配置扫描接口，RequestHandlerSelectors配置如何扫描接口
                .apis(RequestHandlerSelectors.basePackage("com.bdqn.blog.controller"))
                .build();
    }
    //配置文档信息
    private ApiInfo apiInfo() {
        Contact contact = new Contact("阿桐幕", "https://gitee.com/", "2661184062@QQ.com");
        return new ApiInfo(
                "阿桐幕的Swggger", // 标题
                "讨厌自己明明不甘于平凡,却又不好好努力！", // 描述
                "v1.0", // 版本
                "https://www.cnblogs.com/3306La/articles/13790270.html", // 组织链接
                contact, // 联系人信息
                "Apach 2.0 许可", // 许可
                "https://gitee.com/", // 许可连接
                new ArrayList<>()// 扩展
        );
    }

}
